package com.meixiang.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;


/**
 *  重复输入密码键盘
 */
public class EnterNewTwoAgainPaymentKeyDialog extends LinearLayout implements OnItemClickListener {
    //密码输入框
    private GridView mKeyShowGridGridView;
    //键盘view
    private GridView mKeyInputGridGridView;
    private Context context;
    //当前布局
    private View view;
    //键盘适配器
    private KeyGridAdapter keyGridAdapter;
   // private String[] keyList = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "×"};
    private String[] keyList = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", ""};
    private List<String> showList = new ArrayList<>();
    private ShowGridAdapter adapter;
    private String password = "";
    //    private OnCustomDialogListener listener;
    private Display d;
    private Animation animation;
    private Class<?> activity;
    //    private Activity activity;
    private int mColor = Color.parseColor("#00000000");

    public EnterNewTwoAgainPaymentKeyDialog(Context context) {
        this(context, null);
    }

    public EnterNewTwoAgainPaymentKeyDialog(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private TextView tv_product_amount;//键盘的标题
    private int types;
    private String ClassStatus;
    private InputFinishListener inputFinishListener;

    public void setInputFinishListener(InputFinishListener listener) {
        this.inputFinishListener = listener;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public EnterNewTwoAgainPaymentKeyDialog(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        super(context,listener, attrs, defStyle);
        this.context = context;
//        this.listener=listener;
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.payment_key, this, true);
        tv_product_amount = (TextView) view.findViewById(R.id.tv_product_amount);
        initView();
    }

    private void initView() {
        mKeyShowGridGridView = (GridView) view.findViewById(R.id.key_show_grid);
        mKeyInputGridGridView = (GridView) view.findViewById(R.id.key_input_grid);
        mKeyInputGridGridView.startAnimation(
                AnimationUtils.loadAnimation(context,R.anim.pay_open));
        keyGridAdapter = new KeyGridAdapter(keyList, context);
        mKeyInputGridGridView.setAdapter(keyGridAdapter);
        mKeyInputGridGridView.setOnItemClickListener(this);
        for (int i = 0; i < 6; i++) {
            showList.add("");
        }
        adapter = new ShowGridAdapter(context);
        mKeyShowGridGridView.setAdapter(adapter);
        adapter.UpdateData(showList);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            view.startAnimation(animation);
        }
        return super.onKeyDown(keyCode, event);
    }

    public void setKeyTitle(int keyTitle) {
        tv_product_amount.setText(keyTitle);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击数字，添加到集合
        if (showList.get(5).equals("") && position != 9 && position != 11) {
            for (int i = 0; i < showList.size(); i++) {
                if (showList.get(i).equals("")) {
                    showList.set(i, keyList[position]);
                    break;
                }
            }
        } else {
            password = "";
        }
        //点击X，删除后面一位
        if (position == 11) {
            for (int i = showList.size() - 1; i >= 0; i--) {
                if (!showList.get(i).equals("")) {
                    showList.set(i, "");
                    break;
                }
            }
            password = "";
        }
        //更新显示的密码
        adapter.UpdateData(showList);
        //如果输满6位，则调用回调方法
        if (!showList.get(5).equals("")) {
            for (int i = 0; i < showList.size(); i++) {
                password += showList.get(i);
            }

            if (inputFinishListener != null) {
                inputFinishListener.onInputFinish(password);
            }
        }
    }

    public void juumActivity(Class<?> activity, String ClassStatus) {
        this.activity = activity;
        this.ClassStatus = ClassStatus;
        cancelPass();


    }


    /**
     * 清除密码
     */
    public void cancelPass() {
        password = "";
        for (int i = showList.size() - 1; i >= 0; i--) {
            if (!showList.get(i).equals("")) {
                showList.set(i, "");
            }
        }
        adapter.UpdateData(showList);
    }


    //适配器
    class ShowGridAdapter extends BaseAdapter {
        private List<String> showList = new ArrayList<>();
        private LayoutInflater inflater;

        public ShowGridAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void UpdateData(List<String> showList) {
            this.showList = showList;
            notifyDataSetInvalidated();
        }

        @Override
        public int getCount() {
            return showList.size();
        }

        @Override
        public Object getItem(int position) {
            return showList.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.payment_key_itme, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.viewright.setBackgroundResource(R.color.input_box);
            viewHolder.viewright.getLayoutParams().width = DensityUtil.dip2px(context, 1);
            viewHolder.viewtop.setVisibility(View.GONE);
            if (position == 5) {
                viewHolder.viewright.setVisibility(View.GONE);
            }
            if (showList.get(position).equals("")) {
                viewHolder.itemtxt.setText("");
            } else {
                viewHolder.itemtxt.setText("●");
            }
            return convertView;
        }
    }

    class KeyGridAdapter extends BaseAdapter {
        String[] keyList = new String[12];
        private LayoutInflater inflater;

        public KeyGridAdapter(String[] keyList, Context context) {
            this.keyList = keyList;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return keyList.length;
        }

        @Override
        public Object getItem(int position) {
            return keyList[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.payment_key_itme, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (position == 2 || position == 5 || position == 8 || position == 11) {
                viewHolder.viewright.setVisibility(View.GONE);
            } else {
                viewHolder.viewright.setVisibility(View.VISIBLE);
            }
//            if (position == 9) {
//                convertView.setClickable(false);
//                convertView.setBackgroundResource(R.color.white);
//            }
            if (position==11){
                Drawable drawable = ContextCompat.getDrawable(getContext(),R.drawable.key_del);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                viewHolder.itemtxt.setCompoundDrawables(drawable,null,null,null);
            }
            if (position==9){  //setImageResource
                Drawable drawable = ContextCompat.getDrawable(getContext(),R.drawable.keyboard_space);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                viewHolder.itemtxt.setCompoundDrawables(drawable,null,null,null);
                convertView.setClickable(false);
            }

            viewHolder.itemtxt.setText(keyList[position]);
            return convertView;
        }

    }

    public class ViewHolder {
        public final View viewtop;
        public final TextView itemtxt;
        public final View viewright;
        public final View root;

        public ViewHolder(View root) {
            viewtop = (View) root.findViewById(R.id.view_top);
            itemtxt = (TextView) root.findViewById(R.id.item_txt);
            viewright = (View) root.findViewById(R.id.view_right);
            this.root = root;
        }
    }

    /**
     * 输入完成监听事件
     *
     * @author LZRUI
     */
    public interface InputFinishListener {
        void onInputFinish(String pwd);
    }

}
